package jianzhi

import "sort"

func majorityElement(nums []int) int {
	countMap := make(map[int]int)

	for _, v := range nums {
		if _, ok := countMap[v]; ok {
			countMap[v]++
		} else {
			countMap[v] = 1
		}
	}

	res := -1

	for k, v := range countMap {
		if v > len(nums)/2 {
			res = k
		}
	}

	return res

}

func majorityElement2(nums []int) int {
	sort.Ints(nums)
	return nums[len(nums)/2]
}
